package com.bh.ly.oa;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import com.bh.ly.oa.activity.LoginActivity;
import com.bh.ly.oa.util.HttpClent;
import com.bh.ly.oa.util.Util;

public class NotificationService extends Service {
	
	private static final int INTERVAL = 60*1000;
	private Thread noticeThread;
	private boolean isStop = false;

	/**
	 * Class for clients to access. Because we know this service always runs in
	 * the same process as its clients, we don't need to deal with IPC.
	 */
	public class LocalBinder extends Binder {
		NotificationService getService() {
			return NotificationService.this;
		}
	}

	// This is the object that receives interactions from clients. See
	// RemoteService for a more complete example.
	private final IBinder mBinder = new LocalBinder();


	// Unique Identification Number for the Notification.
	// We use it on Notification start, and to cancel it.
	private int NOTIFICATION = R.string.notice_unread_order_num;

	@Override
	public IBinder onBind(Intent arg0) {
		Log.d("NotificationService", "onBind..............");
		return mBinder;
	}

	@Override
	public void onCreate() {
		Log.d("NotificationService", "onCreate..............");
		super.onCreate();


		noticeThread = new Thread() {

			@Override
			public void run() {
				while(!isStop) {
					long currentTime = System.currentTimeMillis();
					if (Util.isNetworkAvailable(NotificationService.this)) {
					    boolean hasUnreadOrder = HttpClent.hasUnreadOrderToday();
					    if (hasUnreadOrder) {
					    	showNotification();
					    }
					}
					currentTime = System.currentTimeMillis() - currentTime;
					if (currentTime < INTERVAL) {
						try {
							Thread.sleep(INTERVAL - currentTime);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				}
			}
			
		};
		noticeThread.start();
		// Display a notification about us starting. We put an icon in the
		// status bar.
		showNotification();
	}

	@Override
	public void onDestroy() {
		Log.d("NotificationService", "onDestroy..............");
		this.isStop = true;
		// Cancel the persistent notification.
		NotificationManager mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		mNM.cancel(NOTIFICATION);

		Toast.makeText(this, R.string.notice_unread_order_num, Toast.LENGTH_SHORT).show();

		super.onDestroy();
	}

	@Override
	public void onRebind(Intent intent) {
		Log.d("NotificationService", "onRebind..............");
		super.onRebind(intent);
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		Log.d("NotificationService", "onStartCommand..............");
		// return super.onStartCommand(intent, flags, startId);
		// We want this service to continue running until it is explicitly
		// stopped, so return sticky.
		return START_STICKY;
	}

	/**
	 * Show a notification while this service is running.
	 */
	private void showNotification() {
		NotificationManager mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		// In this sample, we'll use the same text for the ticker and the
		// expanded notification
		CharSequence title = getText(R.string.app_name);
		// In this sample, we'll use the same text for the ticker and the expanded notification
		CharSequence text = getText(R.string.notice_unread_order_num);
		//(BitmapDrawable)(this.getResources().getDrawable(R.drawable.mobile));

        // Set the icon, scrolling text and timestamp
        Notification notification = new Notification(R.drawable.mobile, text,
                System.currentTimeMillis());

        // The PendingIntent to launch our activity if the user selects this notification
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, LoginActivity.class), 0);

        // Set the info for the views that show in the notification panel.
        notification.setLatestEventInfo(this, title,
                       text, contentIntent);

		// Send the notification.
		mNM.notify(NOTIFICATION, notification);
	}
}
